package znbkCNtxl.CNTXL_DXT;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.znbk.znbklibrary.base.BaseRecyclerAdapter;
import com.example.znbk.znbklibrary.base.BaseRecyclerViewHolder;
import com.example.znbk.znbklibrary.util.LancooUtils;
import com.example.znbk.znbklibrary.widget.ScaleButton;
import com.lancoo.znbkxx.R;

import java.util.ArrayList;
import java.util.List;

import znbkCNtxl.GetNtxlJson.CNtxlPaperEntity;

public class CNtxlDxtAnswerAdapter extends BaseRecyclerAdapter {

    private Context context;
    private List<CNtxlPaperEntity> CNtxlPaperEntity;
    private int quesIndex = -1;
    private CallBackAdapterDxt callBackAdapterDxt;
    private LayoutInflater inflater;

    public CNtxlDxtAnswerAdapter(Context context, List<CNtxlPaperEntity> CNtxlPaperEntity, int quesIndex, CallBackAdapterDxt callBackAdapterDxt){
        this. context=context;
        this.CNtxlPaperEntity = CNtxlPaperEntity;
        this.quesIndex = quesIndex;
        this.callBackAdapterDxt = callBackAdapterDxt;
        inflater=LayoutInflater. from(context);
        getAnswerInfor();
    }

    private int subChildOptionCount = -1;
    private int ChildListSize  =0;
    private void getAnswerInfor(){

        int SubmitState = CNtxlPaperEntity.get(quesIndex).getFavouriteAndNote().getSubmitState();
        int Size = CNtxlPaperEntity.get(quesIndex).getFavouriteAndNote().getChildList().size();
        if(SubmitState==0) {
            for (int i = 0; i < Size; i++) {
                CNtxlPaperEntity.get(quesIndex).getFavouriteAndNote().getChildList().get(i).
                        getSubChildList().get(0).getSubChildItem().get(0).setStuAnswer("");
            }
        }

        subChildOptionCount = CNtxlPaperEntity.get(quesIndex).getQuesFromTK().getChildList().
                                        get(0).getSubChildList().get(0).getOptionList().size();
        ChildListSize = CNtxlPaperEntity.get(quesIndex).getQuesFromTK().getChildList().size();
    }

    @Override
    public int getItemCount() {
        return subChildOptionCount;
    }

    private int choosepos = -1;
    private LinearLayout mLlOption;
    private ScaleButton mSBtnOptionIndex;
    private TextView mTvOptionContent;

    @Override
    public void onBindViewHolder(BaseRecyclerViewHolder baseRecyclerViewHolder, final int position) {

        mLlOption = baseRecyclerViewHolder.getView(R.id.ll_option);

        mSBtnOptionIndex = baseRecyclerViewHolder.getView(R.id.sbtn_optionIndex);
        String OptionIndex = CNtxlPaperEntity.get(quesIndex).getQuesFromTK().getChildList().
                                    get(0).getSubChildList().get(0).getOptionList().get(position).getOption();
        mSBtnOptionIndex.setText(OptionIndex);
        mSBtnOptionIndex.setTag(position);
        mSBtnOptionIndex.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int SubmitState = CNtxlPaperEntity.get(quesIndex).getFavouriteAndNote().getSubmitState();
                if(SubmitState==2){
                    return;
                }
                doRefresh(position);
            }
        });

        mTvOptionContent = baseRecyclerViewHolder.getView(R.id.tv_optionContent);
        String OptionContent = CNtxlPaperEntity.get(quesIndex).getQuesFromTK().getChildList().
                                get(0).getSubChildList().get(0).getOptionList().get(position).getContent();
        mTvOptionContent.setText(OptionContent);
        mTvOptionContent.setTag(position);
        mTvOptionContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                int SubmitState = CNtxlPaperEntity.get(quesIndex).getFavouriteAndNote().getSubmitState();
                if(SubmitState==2){
                    return;
                }
                doRefresh(position);
            }
        });

        String SubChildStuAnswerA = CNtxlPaperEntity.get(quesIndex).getFavouriteAndNote().getChildList().
                get(0).getSubChildList().get(0).getSubChildItem().get(0).getStuAnswer();
        if(SubChildStuAnswerA!=null) {
            if(SubChildStuAnswerA.equals("")){
                choosepos = -1;
            }else {
                choosepos = Integer.valueOf(LancooUtils.stringToAscii(SubChildStuAnswerA)) - 65;
            }
        }else{
            choosepos = -1;
        }
        if (choosepos == position){

            int SubmitState = CNtxlPaperEntity.get(quesIndex).getFavouriteAndNote().getSubmitState();
            String StuAnswer = CNtxlPaperEntity.get(quesIndex).getFavouriteAndNote().getChildList().
                    get(0).getSubChildList().get(0).getSubChildItem().get(0).getStuAnswer();
            String ItemAnswer = CNtxlPaperEntity.get(quesIndex).getFavouriteAndNote().getChildList().
                    get(0).getSubChildList().get(0).getSubChildItem().get(0).getItemAnswer();
            if(SubmitState==2) {

                if (ItemAnswer.equals(StuAnswer)) {
                    mSBtnOptionIndex.setTextColor(context.getResources().getColor(R.color.lancoooWhite));
                    mSBtnOptionIndex.setBackgroundResource(R.drawable.round_green);
                    mTvOptionContent.setTextColor(context.getResources().getColor(R.color.lancoooGreen));
                }else{
                    mSBtnOptionIndex.setTextColor(context.getResources().getColor(R.color.lancoooWhite));
                    mSBtnOptionIndex.setBackgroundResource(R.drawable.round_red);
                    mTvOptionContent.setTextColor(context.getResources().getColor(R.color.lancoooRed));
                }
            }else {
                mSBtnOptionIndex.setTextColor(context.getResources().getColor(R.color.lancoooWhite));
                mSBtnOptionIndex.setBackgroundResource(R.drawable.round_blue);
                mTvOptionContent.setTextColor(context.getResources().getColor(R.color.lancoooBlue));
            }

        }else {
            int SubmitState = CNtxlPaperEntity.get(quesIndex).getFavouriteAndNote().getSubmitState();
            String ItemAnswer = CNtxlPaperEntity.get(quesIndex).getFavouriteAndNote().getChildList().
                    get(0).getSubChildList().get(0).getSubChildItem().get(0).getItemAnswer();
            if(SubmitState==2) {
                int TrueAnswerAscii = Integer.valueOf(LancooUtils.stringToAscii(ItemAnswer)) - 65;
                if(TrueAnswerAscii==position){
                    mSBtnOptionIndex.setTextColor(context.getResources().getColor(R.color.lancoooWhite));
                    mSBtnOptionIndex.setBackgroundResource(R.drawable.round_green);
                    mTvOptionContent.setTextColor(context.getResources().getColor(R.color.lancoooGreen));
                }else{
                    mSBtnOptionIndex.setTextColor(context.getResources().getColor(R.color.lancoooBlack));
                    mSBtnOptionIndex.setBackgroundResource(R.drawable.round_white);
                    mTvOptionContent.setTextColor(context.getResources().getColor(R.color.lancoooBlack));
                }
            }else {
                mSBtnOptionIndex.setTextColor(context.getResources().getColor(R.color.lancoooBlack));
                mSBtnOptionIndex.setBackgroundResource(R.drawable.round_white);
                mTvOptionContent.setTextColor(context.getResources().getColor(R.color.lancoooBlack));
            }
        }

        if(position==subChildOptionCount-1){
            mLlOption.removeAllViews();
            View mView = new View(context);
            mView.setBackgroundColor(Color.parseColor("#ffffff"));
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 300);
            mLlOption.addView(mView, lp);
        }

    }

    @Override
    public View getHolderView(ViewGroup parent, int position) {
        View view = inflater.inflate(R.layout.cntxl_adapter_item_answer_dxt,parent, false);
        return view;
    }

    public void doRefresh(int position){
        choosepos = position;
            //输入学生作答
            if(position!=-1) {
                CNtxlPaperEntity.get(quesIndex).getFavouriteAndNote().getChildList().get(0).
                        getSubChildList().get(0).getSubChildItem().get(0).setStuAnswer(LancooUtils.asciiToString(String.valueOf(65 + position)));
            }

            int AnsweredCount=0;
            for(int i=0;i<ChildListSize;i++){
                String StuAnswer =  CNtxlPaperEntity.get(quesIndex).getFavouriteAndNote().getChildList().
                        get(i).getSubChildList().get(0).getSubChildItem().get(0).getStuAnswer();

                if(!StuAnswer.equals("")){
                    AnsweredCount = AnsweredCount+1;
                }
            }
            //若作答完成则显示答案，记录作答状态，否则亦然
            if (AnsweredCount == ChildListSize) {
                CNtxlPaperEntity.get(quesIndex).getFavouriteAndNote().setSubmitState(2);
                callBackAdapterDxt.callAnsweredAll();
            }else{
                CNtxlPaperEntity.get(quesIndex).getFavouriteAndNote().setSubmitState(1);
            }

        notifyDataSetChanged();
    }

    public interface CallBackAdapterDxt {
        void callAnsweredAll();
    }


}
